import http from '@ohos.net.http';
import ShopInfo from '../viewmodel/ShopInfo';

class ShopModel {
  baseURL: string = 'http://localhost:3000'
  pageNo: number = 1

  getShopList(): Promise<ShopInfo[]> {
    return new Promise((resole, reject) => {
      // 1.创建http的请求对象
      let httpRequest = http.createHttp()
      // 2.发送请求
      httpRequest.request(
        `${this.baseURL}/shops?pageNo=${this.pageNo}&pageSize=3`,
        {
          method: http.RequestMethod.GET
        }
      ).then(result => {
        if (result.responseCode == 200) {
          // 查询成功
          console.log('查询商品成功', JSON.stringify(result.result.toString()))
          resole(
            JSON.parse(result.result.toString())
          )
        } else {
          reject(
            '查询商铺信息失败'
          )
          console.log('查询商铺信息失败', JSON.stringify(result))
        }
      }).catch(error => {
        reject(
          '查询商铺信息失败'
        )
        console.log('查询商铺信息失败', JSON.stringify(error))
      }).finally()
    })
  }
}

const shopModel = new ShopModel()

export default shopModel as ShopModel
